/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testingimpl.testmethodservice;

import entities.TestItem;
import entities.TestResult;
import executor.ExecutionResult;
import java.util.ArrayList;
import java.util.List;


import testing.ITestMethod;
import testing.TestingException;

/**
 *
 * @author ryuju
 */
public class TestMethodDiff implements ITestMethod{

    @Override
    public TestResult doTest(TestItem testItem, ExecutionResult execResult) throws TestingException {
        
        TestResult testResult;
        if(null != execResult)
        {
            testResult = new TestResult();
            testResult.setExecuted(true);
            testResult.setExecutionTime(execResult.execTime);
            testResult.setSubmitID(testItem.getSubmitID());
            
            List<Boolean> resultList = new ArrayList<Boolean>();
            List<String> resultMsgList = new ArrayList<String>();
            
            
            // simple test.. (T.T)
            if(testItem.getTestOutputs().size() != execResult.outputs.length)
            {
                testResult = getFailTestResult(testItem, "The number of execution outputs is not equal to the number of test outputs");
            }
            else
            {
                List<String> testOutputs = testItem.getTestOutputs();
                for(int idx = 0; idx < execResult.outputs.length; idx++)
                {
                    String testOutput = testOutputs.get(idx);
                    String execOutput = execResult.outputs[idx];
                    testOutput = testOutput.trim();
                    execOutput = execOutput.trim();
                    
                    resultMsgList.add("Comapre : (" + testOutput + ", " + execOutput + ")");
                    
                    if(testOutput.equals(execOutput))
                    {
                        resultList.add(true);
                    }
                    else
                    {
                        resultList.add(false);
                    }
                    
                }
            }
            
            testResult.setTestResultMsgs(resultMsgList);
            testResult.setTestResults(resultList);
            
        }
        else
        {
            testResult = getFailTestResult(testItem, "empty execution result.");
        }
        
        return testResult;
    }
    
    private TestResult getFailTestResult(TestItem testItem, String msg)
    {
        TestResult testResult = new TestResult();
        testResult.setErrMsg(msg);;
        testResult.setSubmitID(testItem.getSubmitID());
        testResult.setExecuted(false);
        
        return testResult;
    }
    
}
